/**
 * 核心技术支持
 * IT渠道网 小程序商城
 * 13873174181 微信同号
 */
import {Nav} from '../../utils/nav.js';
import {My} from '../my/my-model.js';
var nav = new Nav();
var my = new My();

Page({
    data: {
        isRuleTrue: false,
        userInfo: null,
        pageIndex: 1,
        loadingHidden: false,
        isLoadedAll: false,
        signArr: [],
        totalPage: 0,
        isSign: false,
        showSign: false,
        showRule: false,
        result: {},
    },
    onLoad: function() {
        this._findSign();
    },
     /**
     * 加载数据
     */
    onShow: function() {
        this._loadHead();
    },
    /**
     * 用户信息
     */
    _loadHead: function() {
      var that = this;
      // 查看是否授权
      wx.getSetting({
        success: function(res){
          if (res.authSetting['scope.userInfo']) {
            my.getUserInfo((data)=>{
              that.setData({
                userInfo: data
              })
            })
          } else {
            that.setData({
              isRuleTrue: true
            })
          }
        }
      })
    },
    /**
     * bind
     */
    onGotUserInfo: function (e) {
      var that = this;
      that.setData({
        isRuleTrue: false
      })
      if (e.detail.errMsg == 'getUserInfo:ok') {
        my.getUserInfo((data)=>{
          that.setData({
            userInfo: data
          })
        }, 1)
      } else {
        that.setData({
          isRuleTrue: true
        })
      }
    },
    /**
     * 获取签到积分记录
     */
    _findSign: function(i) {
        var that = this,
            page = this.data.pageIndex;
        nav.findSign(page, (data)=>{
            if (data.list[0]) {
                var fday = ((new Date(new Date().toLocaleDateString()).getTime()+24*60*60*1000-1)-new Date(data.list[0].created).getTime())/(1000*60*60*24);
                if(fday<1){
                    that.setData({
                        isSign: true
                    })
                }
            }
            data.list.forEach((item) => {
                item.tochange = item.tochange.substring(1);
            })
            if (i!=1) {
                that.data.signArr.push.apply(that.data.signArr, data.list);
                that.setData({
                    signArr: that.data.signArr,
                    totalPage: data.totalPage,
                    loadingHidden: true
                })
            } else {
                that.setData({
                    signArr: data.list,
                    totalPage: data.totalPage
                })
            }
            
        })
    },
    /**
     * 签到
     */
    signIn: function() {
        var that = this;
        if(!that.data.isSign){
            nav.signIn((data)=>{
                if(data.success){
                    that.setData({
                        showSign: true,
                        isSign: true,
                        result: data
                    })
                    that._findSign(1);
                }else{
                    wx.showToast({
                      title: data.message,
                      icon: 'none',
                      duration: 1000
                    })
                }
            })
        }
    },
    /**
     * 签到反馈
     */
    showSign: function() {
        this.setData({showSign: false})
    },
    /**
     * 规则
     */
    showRule: function(e) {
        var i = nav.getDataSet(e, 'in');
        if (i==1) {
            this.setData({showRule: true})
        } else {
            this.setData({showRule: false})
        }
    },
    /**
     * 监听用户下拉动作 重新刷新用户收藏,回到&page=1
     */
    onPullDownRefresh: function() {
        var that = this;
        that.data.signArr = [];  // 初始化
        that.data.pageIndex = 1;
        that._getStoreList(() => {
            that.setData({
                isLoadedAll: false
            });
            wx.stopPullDownRefresh()
        })
    },
    /**
     * 页面上拉触底事件的处理函数 上拉到底部触发&page=totalPage+1
     */
    onReachBottom: function() {
        if (!this.data.isLoadedAll && this.data.pageIndex < this.data.totalPage) {
            this.data.pageIndex++;
            this._getStoreList();
        }
        if (this.data.pageIndex == this.data.totalPage){
            this.setData({
                isLoadedAll: true
            })
        }
    }
})